/*
 * D.A.O. for JPA  
 * Created on 6 juin 2013 ( Time 15:03:52 )
 */

package org.demo.typeabonnementoption.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.telosys.starterkits.strutsjpa.CoreUtils;
import org.telosys.starterkits.strutsjpa.GenericJpaDao;

import org.demo.typeabonnementoption.bean.Typeabonnementoption;
import org.demo.typeabonnementoption.bean.TypeabonnementoptionKey;

public class JpaTypeabonnementoptionDAO extends GenericJpaDao<Typeabonnementoption, TypeabonnementoptionKey> {

	private static final Predicate[] ap = {};
	
	/**
	 * Constructs a DAO for Typeabonnementoption
	 */
	public JpaTypeabonnementoptionDAO() {
		super(Typeabonnementoption.class);
	}
	
	/**
	 * Search by Typeabonnementoption example
	 */
	public List<Typeabonnementoption> search(final Typeabonnementoption element) {
		final EntityManager em = getEntityManager();
		if ( element != null  ) {
			CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
			CriteriaQuery<Typeabonnementoption> criteriaQuery = criteriaBuilder.createQuery(Typeabonnementoption.class);
			Root<Typeabonnementoption> from = criteriaQuery.from(Typeabonnementoption.class);

			List<Predicate> listP = new ArrayList<Predicate>();

			if ( element.getKey() != null  ) {
				if (CoreUtils.isNotBlank(element.getKey().getIdtypeabonnement())) {
					Predicate predicate = criteriaBuilder.equal(from.get("idtypeabonnement"), element.getKey().getIdtypeabonnement());
					listP.add(predicate);
				}
				if (CoreUtils.isNotBlank(element.getKey().getIdoption())) {
					Predicate predicate = criteriaBuilder.equal(from.get("idoption"), element.getKey().getIdoption());
					listP.add(predicate);
				}
			}

			
			criteriaQuery.where(listP.toArray(ap));
			TypedQuery<Typeabonnementoption> tq = em.createQuery(criteriaQuery);
			return tq.getResultList();
		}
		else
		{
    		return this.loadAll();
		}
	}

}